<?php

namespace App\System\Cis\Repositories;

use App\Module\Api\Controllers\Request\AddCardRequest;
use App\Module\Api\Controllers\Request\TableRequest;
use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Cis\Model\CisBankAccount;
use App\System\Cis\Model\CisUser;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;


class CisBankAccountRepository extends Repository
{
    use SinglePattern;

    /**
     * 银行账户列表
     * @param int $userId
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */

    public function getlist($userId = 0)
    {
        $list = CisBankAccount::with(['bank'])->where('user_id', $userId)->get();
        $data = $list->toArray();
        return $data;
    }

    /**
     * 绑定银行卡
     * @param string $password
     * @param array $data
     * @return array
     */
    public function bindBank($password = '',array $data){
        $userId = Session::get('userid');
        $userInfo = CisUser::find($userId);
        if(!Hash::check($password,$userInfo->password)){
            return ['s'=>0,'m'=>'登录密码错误'];
        }
        $data['user_id'] = $userId;
        $bool = CisBankAccount::create($data);
        if($bool){
            return ['s'=>1];
        }else{
            return ['s'=>0,'m'=>'操作失败'];
        }
    }

    /**
     * 添加编辑银行卡
     * @param AddCardRequest $addCardRequest
     * @param $user_id
     * @return array
     */
    public function updateData(AddCardRequest $addCardRequest,$user_id)
    {
        $set = ['account_name'=>$addCardRequest->accountName,
                'account_bank_id'=>$addCardRequest->accountBankId,
                'account_branch_name'=>$addCardRequest->bankBranchName,
                'account_code'=>$addCardRequest->accountIdCode,
                'user_id'=>$user_id];
        $data = ['s'=>'0','d'=>'操作成功'];
        if($addCardRequest->id)
        {
            $res = CisBankAccount::where('id',$addCardRequest->id)->where("user_id",$user_id)->update($set);
            if(!$res)
            {
                $data['s'] = 1000;
                $data['d'] = '更新失败';
                return $data;
            }

        }
        else
        {
            $check = CisBankAccount::where('user_id',$user_id)->first();
            if($check)
            {
                $data = ['s'=>'10000','d'=>'添加失败，只能添加一张银行卡噢'];
                return $data;
            }
            $res = CisBankAccount::create($set);
            if(!$res)
            {
                $data['s'] = 1000;
                $data['d'] = '添加失败';
                return $data;
            }
            $data['id'] = $res->id;

        }
        return $data;
    }

    /**
     * 银行卡列表
     * @param $request object
     * @return mixed
     */
    public function getApiList(TableRequest $request,$user_id)
    {
        $data = CisBankAccount::where('user_id',$user_id)
            ->orderBy($request->orderBy,$request->orderType)
            ->paginate($request->limit,['*'],'page',$request->page);
        foreach($data as $v) {
            $item = [];
            $item['id'] = $v->id;
            $item['accountIdCode'] = $v->account_code;
            $item['accountName'] = $v->account_name;
            $item['accountBankId'] = $v->account_bank_id;
            $item['accountBankName'] = $v->account_branch_name;
            $return['cards'][] = $item;
        }
        $return['total'] =$data->total();
        return $return;
    }

    /**
     * 查询一条银行卡信息
     * @param $id
     * @param $user_id
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function getOne($id,$user_id)
    {
       return CisBankAccount::with(['bank'])
       ->where('id',$id)->where("user_id",$user_id)->first();
    }
}